package com.guide.dao.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.guide.bizdto.AccountSel;
import com.guide.dao.AccountDao;
import com.guide.domain.Account;
import com.guide.domain.AccountQuery;
import com.guide.domain.AccountQuery.Criteria;
import com.guide.persistence.AccountMapper;
import com.guide.utils.ConstantUtils;
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
	private static final Logger logger = LoggerFactory
			.getLogger(AccountDaoImpl.class);
	
	@Autowired
	private AccountMapper accountMapper;
	
	@Override
	public long insertAccount(Account account) {
		// TODO Auto-generated method stub
		return accountMapper.insert(account);
	}

	@Override
	public Account getAccountByName(String name) {
		AccountQuery query = new AccountQuery();
		query.createCriteria().andUserNameEqualTo(name);
		List<Account> accounts = accountMapper.selectByExample(query,null,null);
		if(!CollectionUtils.isEmpty(accounts)) {
			return accounts.get(0);
		}
		return null;
	}

	@Override
	public Account getAccountById(Long userId) {
		// TODO Auto-generated method stub
		return accountMapper.selectByPrimaryKey(userId);
	}

	@Override
	public List<Account> getAccountList(AccountSel accountsel) {
		AccountQuery query = new AccountQuery();
		query.setOrderByClause("ctime desc");
		Criteria criteria = query.createCriteria();
		if(accountsel.getStatus()!=null){
			criteria.andStatusEqualTo(accountsel.getStatus());
		}
		if(accountsel.getUserType()!=null){
			criteria.andUserTypeEqualTo(accountsel.getUserType());
		}else{
			criteria.andUserTypeNotEqualTo(ConstantUtils.ACCOUNT_TYPE_ADMIN);
		}
		
		return accountMapper.selectByExample(query, accountsel.getPageIndex()*accountsel.getPageSize(), accountsel.getPageSize());
	}

	@Override
	public Integer countAccount(AccountSel accountsel) {
		// TODO Auto-generated method stub
		AccountQuery query = new AccountQuery();
		Criteria criteria = query.createCriteria();
		if(accountsel.getStatus()!=null){
			criteria.andStatusEqualTo(accountsel.getStatus());
		}
		if(accountsel.getUserType()!=null){
			criteria.andUserTypeEqualTo(accountsel.getUserType());
		}else{
			criteria.andUserTypeNotEqualTo(ConstantUtils.ACCOUNT_TYPE_ADMIN);
		}
		return accountMapper.countByExample(query);
	}
	@Override
	public void modifyAccount(Account account) {
		// TODO Auto-generated method stub
		accountMapper.updateByPrimaryKeySelective(account);
	}

	


	
	



}
